package com.ruoyi.ma.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ma.domain.bo.MaBaseManifestStandardBo;
import com.ruoyi.ma.domain.vo.MaBaseManifestStandardVo;
import com.ruoyi.ma.domain.MaBaseManifestStandard;
import com.ruoyi.ma.mapper.MaBaseManifestStandardMapper;
import com.ruoyi.ma.service.IMaBaseManifestStandardService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 甲供材标准材料清单Service业务层处理
 *
 * @author tll
 * @date 2024-11-26
 */
@RequiredArgsConstructor
@Service
public class MaBaseManifestStandardServiceImpl implements IMaBaseManifestStandardService {

    private final MaBaseManifestStandardMapper baseMapper;

    /**
     * 查询甲供材标准材料清单
     */
    @Override
    public MaBaseManifestStandardVo queryById(Long maStandardId){
        return baseMapper.selectVoById(maStandardId);
    }

    /**
     * 查询甲供材标准材料清单列表
     */
    @Override
    public TableDataInfo<MaBaseManifestStandardVo> queryPageList(MaBaseManifestStandardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaBaseManifestStandard> lqw = buildQueryWrapper(bo);
        Page<MaBaseManifestStandardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询甲供材标准材料清单列表
     */
    @Override
    public List<MaBaseManifestStandardVo> queryList(MaBaseManifestStandardBo bo) {
        LambdaQueryWrapper<MaBaseManifestStandard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaBaseManifestStandard> buildQueryWrapper(MaBaseManifestStandardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaBaseManifestStandard> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getType()), MaBaseManifestStandard::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTypeName()), MaBaseManifestStandard::getTypeName, bo.getTypeName());
        lqw.eq(bo.getSort() != null, MaBaseManifestStandard::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getManifestCode()), MaBaseManifestStandard::getManifestCode, bo.getManifestCode());
        lqw.like(StringUtils.isNotBlank(bo.getManifestName()), MaBaseManifestStandard::getManifestName, bo.getManifestName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), MaBaseManifestStandard::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getSpecification()), MaBaseManifestStandard::getSpecification, bo.getSpecification());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), MaBaseManifestStandard::getUnit, bo.getUnit());
        lqw.eq(bo.getManifestNumber() != null, MaBaseManifestStandard::getManifestNumber, bo.getManifestNumber());
        lqw.eq(bo.getManifestPrice() != null, MaBaseManifestStandard::getManifestPrice, bo.getManifestPrice());
        lqw.eq(bo.getManifestTotal() != null, MaBaseManifestStandard::getManifestTotal, bo.getManifestTotal());
        lqw.eq(bo.getAdjustments() != null, MaBaseManifestStandard::getAdjustments, bo.getAdjustments());
        return lqw;
    }

    /**
     * 新增甲供材标准材料清单
     */
    @Override
    public Boolean insertByBo(MaBaseManifestStandardBo bo) {
        MaBaseManifestStandard add = BeanUtil.toBean(bo, MaBaseManifestStandard.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMaStandardId(add.getMaStandardId());
        }
        return flag;
    }

    /**
     * 修改甲供材标准材料清单
     */
    @Override
    public Boolean updateByBo(MaBaseManifestStandardBo bo) {
        MaBaseManifestStandard update = BeanUtil.toBean(bo, MaBaseManifestStandard.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaBaseManifestStandard entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除甲供材标准材料清单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 查询甲供材标准材料清单列表不分页
     *
     * @param bo
     * @return
     */
    @Override
    public List<MaBaseManifestStandardVo> allList(MaBaseManifestStandardBo bo) {
        LambdaQueryWrapper<MaBaseManifestStandard> lqw = buildQueryWrapper(bo);
        List<MaBaseManifestStandardVo> result = baseMapper.selectVoList(lqw);
        return result;
    }
}
